@echo off
setlocal

REM Create logs directory if it doesn't exist
set LOG_DIR=logs
if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"

REM Generate timestamp (avoiding illegal characters in filename)
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "TIMESTAMP=%dt:~0,4%%dt:~4,2%%dt:~6,2%_%dt:~8,2%%dt:~10,2%%dt:~12,2%"

REM Set log prefix based on arguments
set LOG_PREFIX=log

REM Set log file path
set LOG_FILE=%LOG_DIR%\%LOG_PREFIX%_%TIMESTAMP%.log

REM Display log file path
echo Log file: %LOG_FILE%

REM Run node command with output redirection to both console and log file with timestamps
REM Using PowerShell with proper encoding to handle Chinese characters and add timestamps
powershell.exe -Command "& { $OutputEncoding = New-Object -typename System.Text.UTF8Encoding; [Console]::OutputEncoding = New-Object -typename System.Text.UTF8Encoding; node %* 2>&1 | ForEach-Object { $timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'; $logEntry = \"[$timestamp] $_\"; Write-Output $logEntry; Add-Content -Path '%LOG_FILE%' -Value $logEntry } }"

REM Display exit code
echo.
echo Process exited with code: %errorlevel%